//
//  MyOrderViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "MyOrderViewController.h"

#import "MyOrderCell.h"
#import "OrderDetCell.h"
#import "OrderDetViewController.h"
@interface MyOrderViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UILabel *labLine;
    UIButton *btnL;
    UIButton *btnR;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableDictionary *dic;

@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBGColor];
    _dic = [NSMutableDictionary dictionary];
    
    [self loadNav];
    [self loadUI];
}
//自定义导航栏
- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    //返回按钮
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnBack];
    [btnBack makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.left);
        make.bottom.equalTo(view.bottom);
        make.width.height.equalTo(40);
    }];
    
    btnL = [UIButton buttonWithType:UIButtonTypeCustom];
    //btnL.backgroundColor = [UIColor grayColor];
    btnL.frame = CGRectMake(ScreenWidth/2-75, 30, 65, 25);
    [btnL setTitle:@"我的订单" forState:UIControlStateNormal];
    [btnL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btnL.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [btnL addTarget:self action:@selector(clickL) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnL];
    
    btnR = [UIButton buttonWithType:UIButtonTypeCustom];
    //btnR.backgroundColor = [UIColor orangeColor];
    btnR.frame = CGRectMake(ScreenWidth/2+10, 30, 65, 25);
    [btnR setTitle:@"交易明细" forState:UIControlStateNormal];
    [btnR setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btnR.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [btnR addTarget:self action:@selector(clickR) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnR];
    
    labLine = [[UILabel alloc] initWithFrame:CGRectMake(btnL.frame.origin.x+18, 60, 30, 4)];
    labLine.backgroundColor = [UIColor redColor];
    labLine.layer.cornerRadius = 10.0;
    [view addSubview:labLine];
    
    UILabel *labL = [[UILabel alloc] initWithFrame:CGRectMake(0, 63.5, ScreenWidth, 0.5)];
    labL.backgroundColor = COLOR_RGB(217, 217, 217, 1);
    [view addSubview:labL];
    
}

- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}
//交易明细
- (void)clickL{
    [UIView animateWithDuration:0.3 animations:^{
        labLine.frame = CGRectMake(btnL.frame.origin.x+18, 60, 30, 4);
    }];
    [_dic setObject:@"aaa" forKey:@"tag"];
    [_tableView reloadData];
}
//我的订单
- (void)clickR{
    [UIView animateWithDuration:0.3 animations:^{
        labLine.frame = CGRectMake(btnR.frame.origin.x+18, 60, 30, 4);
    }];
    [_dic setObject:@"sss" forKey:@"tag"];
    [_tableView reloadData];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //去分割线
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [_dic setObject:@"aaa" forKey:@"tag"];
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 15;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = [_dic objectForKey:@"tag"];
    
    if ([str isEqualToString:@"aaa"]) {
        static NSString *cellId = @"Cell1";
        MyOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MyOrderCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.btnFinish.layer.borderWidth = 0.5;
        cell.btnFinish.layer.borderColor = [UIColor redColor].CGColor;
        
        cell.btnReset.layer.borderWidth = 0.5;
        cell.btnReset.layer.borderColor = [UIColor redColor].CGColor;
        
        cell.btnCancel.layer.borderWidth = 0.5;
        cell.btnCancel.layer.borderColor = [UIColor redColor].CGColor;
        
        return cell;
    }else{
        static NSString *cellId = @"Cell2";
        OrderDetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderDetCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = [_dic objectForKey:@"tag"];
    
    if ([str isEqualToString:@"aaa"]) {
        return 145.0;
    }else{
        return 60.0;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        
    }else{
        OrderDetViewController *det = [[OrderDetViewController alloc] init];
        [self.navigationController pushViewController:det animated:YES];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    headView.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 1)];
    lab.backgroundColor = COLOR_RGB(242, 242, 242, 0.5);
    [headView addSubview:lab];
    
    NSArray *array = @[@"全部分类",@"完成状态",@"下单时间"];
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        
        for (NSInteger i = 0; i<array.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(ScreenWidth/3*i, 0, ScreenWidth/3, 40.0);
            btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
            [btn setTitle:[NSString stringWithFormat:@"%@",array[i]] forState:UIControlStateNormal];
            [btn setTitleColor:COLOR_RGB(77, 77, 77, 1) forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn.imageView.frame.size.width, 0, btn.imageView.frame.size.width)];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width+5, 0, -btn.titleLabel.frame.size.width)];
            btn.tag = i;
            [btn addTarget:self action:@selector(clicks:) forControlEvents:UIControlEventTouchUpInside];
            [headView addSubview:btn];
        }
        
    }else{
        UIButton *btnAll = [UIButton buttonWithType:UIButtonTypeCustom];
        btnAll.frame = CGRectMake(0, 0, ScreenWidth, 40.0);
        [btnAll setTitle:@"全部明细" forState:UIControlStateNormal];
        [btnAll setTitleColor:COLOR_RGB(77, 77, 77, 1) forState:UIControlStateNormal];
        btnAll.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [btnAll setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
        [btnAll setTitleEdgeInsets:UIEdgeInsetsMake(0, -btnAll.imageView.frame.size.width, 0, btnAll.imageView.frame.size.width)];
        [btnAll setImageEdgeInsets:UIEdgeInsetsMake(0, btnAll.titleLabel.frame.size.width+5, 0, -btnAll.titleLabel.frame.size.width)];
        [headView addSubview:btnAll];
    }
    
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

- (void)clicks:(UIButton *)btn{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
